package se.thoka.twt.client.widgets;

import se.thoka.twt.client.event.CountDownEvent;
import se.thoka.twt.client.event.CountDownHandler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class ProgressBar extends Composite implements HasMouseMoveHandlers, CountDownHandler {

	interface Binder extends UiBinder<Widget, ProgressBar> {}
	
	private static Binder binder;
	
	@UiField Widget bar;
	
	public ProgressBar() {
		initWidget(getBinder().createAndBindUi(this));
	}
	
	public void setProgress(double progress) {
		bar.setWidth(progress * 100.0 + "%");
	}
	
	protected UiBinder<Widget, ProgressBar> getBinder() {
		return binder != null ? binder : GWT.<Binder>create(Binder.class);
	}

	@Override
	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return addDomHandler(handler, MouseMoveEvent.getType());
	}
	
	@Override
	public void onCountDown(CountDownEvent event) {
		setProgress(event.getProgress());
	}
}
